"""
写文件

Version: 0.1
Author: yuwl
Date：2024-4-07
"""


"""判断素数的函数"""
def is_prime(n):
    assert n > 0
    for factor in range(2, n):
        if n % factor == 0 and factor != 1 and factor != n:
            return False
    return True if n != 1 else False

def main():
    filenames = ('a.txt', 'b.txt', 'c.txt')
    fs_list = []
    try:
        for filename in filenames:
            # 文件不存在，会自动先创建
            fs_list.append(open(filename, 'w', encoding='utf-8'))
        for num in range(1, 10000):
            if is_prime(num):
                if num < 100:
                    fs_list[0].write(str(num) + '\n')
                elif num < 1000:
                    fs_list[1].write(str(num) + '\n')
                else:
                    fs_list[2].write(str(num) + '\n')
    except IOError as e:
        print(f'写文件时发生错误：{e}')
    finally:
        for fs in fs_list:
            fs.close()
    print('操作完成')


if __name__ == "__main__":
    main()